require File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/sourdough/app/helpers/application_helper')

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def avatar_for(user)
    if user.photo
      link_to_unless_current image_tag(user.photo.public_filename(:thumb), :class => 'avatar'), profile_path(user.login)
    else
      link_to_unless_current image_tag('no_photo_thumbnail.gif', :class => 'avatar'), profile_path(user.login)
    end
  end

  def can_display_friend_request_for(profile)
    if logged_in?
      unless current_user.friends_with?(profile) or profile == current_user or current_user.pending_friendships.include? profile or current_user.requested_friendships.include? profile
        yield
      end
    end
  end

  def date(value)
    value.strftime('%B %d, %Y') if value
  end

  def year(value)
    if value
      value.strftime('%Y')
    else
      'Present'
    end
  end

  def month_year(value)
    if value
      value.strftime('%B %Y')
    else
      'Present'
    end
  end

  def breadcrumbs
    s = [link_to_unless_current(@college.name, college_discipline_path(@college))] if @college
    s << link_to_unless_current(@discipline.name, discipline_path(@discipline)) if @discipline
    s << @post.title if @post
    "<p>" + s.join(' &#187; ') + "</p>"
  end

  def link_to_with_span(name, options = { }, html_options = nil, *parameters_for_method_reference)
    if html_options
      html_options = html_options.stringify_keys
      convert_options_to_javascript!(html_options)
      tag_options = tag_options(html_options)
    else
      tag_options = nil
    end

    url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference)
    "<a href=\"#{ url}\"#{ tag_options}><span>#{ name || url}</span></a>"
  end

  def link_to_action(name, options = { }, html_options = nil, *parameters_for_method_reference)
    "<li>#{ link_to( "&#187; #{ name }", options, html_options, *parameters_for_method_reference )}</li>"
  end

  # sometimes we want to say "Joe's page" or "Your page", depending on who's logged in
  # this method returns:
  # - the supplied "text" variable if @user and @current_user are the same (that is, if the user is looking at his own resources)
  # - @user.name (optionally appended with the "append" variable) otherwise
  def user_name_or_text( text, *append )
    return text if @current_user == @user
    return @user.name if append.empty?
    return "#{ @user.name }#{ append }"
  end

  # XXX should be using logged_in? here
  def current_user?
      @user and @current_user and @user == @current_user ? true : false
  end

  def show_document(document)
    link = link_to document.filename, document.public_filename
    link << " (#{document.pretty_content_type}, #{document.size / 1024}K)"
    link << " <small>uploaded #{time_ago_in_words document.created_at} ago </small>"
  end

  def discipline_blurb
    if logged_in?
      blurb = <<EOF
Join groups in your discipline!  Pronetos is organized into colleges and disciplines.  Don't see your discipline?
#{link_to 'Create it', new_discipline_path} then #{link_to 'invite your colleagues', new_user_invite_path(current_user)}.
EOF
    else
      blurb = <<EOF
      Join groups in your discipline! Pronetos is organized into colleges  and disciplines. Don't see your discipline? #{link_to 'Sign up', new_user_path} as a new user, create your discipline, and invite your colleagues.
EOF
    end
    blurb
  end
end
